home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Headers / ANSI Headers / strstream < prev    next >
Encoding:
Text File  |  1994-10-20  |  3.1 KB  |  119 lines  |  [TEXT/MMCC]

  1. // strstream standard header
  2. #ifndef _STRSTREAM_
  3. #define _STRSTREAM_
  4. #include <istream>
  5. #include <ostream>
  6.  
  7. #if __MWERKS__
  8. #pragma options align=mac68k
  9. #endif
  10.  
  11.   // constants
  12. const int _ALSIZE = 512; // default allocation size
  13. const int _MINSIZE = 32; // minimum allocation size
  14.   // class strstreambuf
  15. class strstreambuf : public streambuf {
  16. public:
  17.  enum __Strstate {_Allocated = 1, _Constant = 2,
  18.   _Dynamic = 4, _Frozen = 8, _Noread = 16,
  19.   _Strzero = 0};
  20.  _BITMASK(__Strstate, _Strstate);
  21.  strstreambuf(int _N = 0)
  22.   {_Init(_N); }
  23.  strstreambuf(void *(*_A)(size_t), void (*_F)(void *))
  24.   {_Init(), _Palloc = _A, _Pfree = _F; }
  25.  strstreambuf(char *_G, int _N, char *_P = 0,
  26.   _Strstate _S = _Strzero)
  27.   {_Init(_N, _G, _P, _S); }
  28.  strstreambuf(unsigned char *_G, int _N,
  29.   unsigned char *_P = 0)
  30.   {_Init(_N, (char *)_G, (char *)_P); }
  31.  strstreambuf(const char *_G, int _N)
  32.   {_Init(_N, (char *)_G, 0, _Constant); }
  33.  strstreambuf(const unsigned char *_G, int _N)
  34.   {_Init(_N, (char *)_G, 0, _Constant); }
  35.  virtual ~strstreambuf();
  36.  void freeze(_Bool = 1);
  37.  char *str()
  38.   {freeze(); return (gptr()); }
  39.  int pcount() const
  40.   {return (pptr() == 0 ? 0 : pptr() - pbase()); } 
  41. #if _HAS_SIGNED_CHAR
  42.  strstreambuf(signed char *_G, int _N, signed char *_P = 0)
  43.   {_Init(_N, (char *)_G, (char *)_P); }
  44.  strstreambuf(const signed char *_G, int _N)
  45.   {_Init(_N, (char *)_G, 0, _Constant); }
  46. #endif /* _HAS_SIGNED_CHAR */
  47. protected:
  48.  virtual int overflow(int = EOF);
  49.  virtual int pbackfail(int = EOF);
  50.  virtual int underflow();
  51.  virtual streampos seekoff(streamoff, ios::seekdir,
  52.   ios::openmode = ios::in | ios::out);
  53.  virtual streampos seekpos(streampos,
  54.   ios::openmode = ios::in | ios::out);
  55.  void _Init(int = 0, char * = 0, char * = 0,
  56.   _Strstate = _Strzero);
  57.  void _Tidy();
  58.  _Strstate _Strmode;
  59. private:
  60.  char *_Pendsave, *_Seekhigh;
  61.  int _Alsize;
  62.  void *(*_Palloc)(size_t);
  63.  void (*_Pfree)(void *);
  64.  };
  65. _BITMASK_OPS(strstreambuf::__Strstate)
  66.   // class istrstream
  67. class istrstream : public istream {
  68. public:
  69.  istrstream(const char *_S)
  70.   : ios(&_Sb), istream(&_Sb), _Sb(_S, 0) {}
  71.  istrstream(const char *_S, int _N)
  72.   : ios(&_Sb), istream(&_Sb), _Sb(_S, _N) {}
  73.  istrstream(char *_S)
  74.   : ios(&_Sb), istream(&_Sb), _Sb((const char *)_S, 0) {}
  75.  istrstream(char *_S, int _N)
  76.   : ios(&_Sb), istream(&_Sb), _Sb((const char *)_S, _N) {}
  77.  virtual ~istrstream();
  78.  strstreambuf *rdbuf() const
  79.   {return ((strstreambuf *)&_Sb); }
  80.  char *str()
  81.   {return (_Sb.str()); }
  82. private:
  83.  strstreambuf _Sb;
  84.  };
  85.   // class ostrstream
  86. class ostrstream : public ostream {
  87. public:
  88.  ostrstream()
  89.   : ios(&_Sb), ostream(&_Sb), _Sb() {}
  90.  ostrstream(char *, int, openmode = out);
  91.  virtual ~ostrstream();
  92.  strstreambuf *rdbuf() const
  93.   {return ((strstreambuf *)&_Sb); }
  94.  void freeze(int _F = 1)
  95.   {_Sb.freeze(_F); }
  96.  char *str()
  97.   {return (_Sb.str()); }
  98.  int pcount() const
  99.   {return (_Sb.pcount()); }
  100. private:
  101.  strstreambuf _Sb;
  102.  };
  103.  
  104. #if __MWERKS__
  105. #pragma options align=reset
  106. #endif
  107.  
  108. #endif
  109.  
  110. /*
  111.  * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  112.  * Consult your license regarding permissions and restrictions.
  113.  */
  114.  
  115. /* Change log:
  116.  *94Oct03 Version received from PlumHall
  117.  *94Oct08 Inserted MW changes.
  118.  */
  119.